<template>
  <div class="app-container">
    <el-row :gutter="20">
      <splitpanes :horizontal="this.$store.getters.device === 'mobile'" class="default-theme">
        <!--分类数据-->
        <pane size="16">
          <el-col>
            <el-header style="height: 40px;line-height: 40px;padding: 0 20px;background-color: #f2f2f2;border-bottom: 1px solid #eee;text-align:center">商品分类</el-header>
            <div class="head-container">
              <el-tree :data="categoryList" :props="defaultProps" @node-click="handleNodeClick" />
            </div>
          </el-col>
        </pane>
        <!--修改分类-->
        <pane size="54">
          <el-col>

            <el-header style="height: 40px;line-height: 40px;padding: 0 20px;background-color: #f2f2f2;border-bottom: 1px solid #eee;text-align:center">分类信息</el-header>
            <div style="margin: 20px">
              <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="分类名称" prop="mobile">
                  <el-input v-model="form.name" placeholder="请输入分类名称" :style="{width: '100%'}"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="field101">
                  <el-input v-model="form.sort" placeholder="请输入排序" type="number" :style="{width: '100%'}">
                  </el-input>
                </el-form-item>
                <el-form-item size="large">
                  <el-button type="success" @click="handleAdd">新增</el-button>
                  <el-button type="primary" @click="submitForm">修改</el-button>
                  <el-button type="warning" @click="handleDelete">删除</el-button>
                </el-form-item>
              </el-form>
            </div>
          </el-col>
        </pane>
      </splitpanes>
    </el-row>

    <!-- 添加新分类 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="formAdd" :model="formAdd" label-width="80px">

        <el-form-item label="上级分类" prop="fid" >
          <el-cascader v-model="formAdd.fid"
                       :options="categoryList"
                       :props="defaultProps"
                       clearable></el-cascader>
        </el-form-item>

        <el-form-item label="分类名称" prop="name" required>
          <el-input v-model="formAdd.name" placeholder="请选择上级分类"  />
        </el-form-item>

        <el-form-item label="排序" prop="sort" required>
          <el-input v-model="formAdd.sort" placeholder="请输入排序" type="number"  />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAddForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listCategroy, getCategroy, delCategroy, addCategroy, updateCategroy,getJsonData } from "@/api/system/categroy"
import Treeselect from "@riophae/vue-treeselect"
import "@riophae/vue-treeselect/dist/vue-treeselect.css"
import { Splitpanes, Pane } from "splitpanes"
import "splitpanes/dist/splitpanes.css"

export default {
  name: "categroy",
  components: { Treeselect, Splitpanes, Pane },
  data() {
    return {
      //分类列表
      categoryList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: "",
      // 所有部门树选项
      deptOptions: undefined,
      // 过滤掉已禁用部门树选项
      enabledDeptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 默认密码
      initPassword: undefined,
      // 日期范围
      dateRange: [],
      // 岗位选项
      postOptions: [],
      // 角色选项
      roleOptions: [],
      // 表单参数
      form: {},
      formAdd: {},
      defaultProps: {
        children: "children",
        label: "name",
        value: "id",
        checkStrictly: true
      },

    }
  },
  created() {
    this.getList()
    this.getDeptTree()
    this.getConfigKey("sys.user.initPassword").then(response => {
      this.initPassword = response.msg
    })
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true
      getJsonData().then(response => {
        console.log("分类数据：",response)
        this.categoryList = response.data
        this.loading = false
      })
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data
        this.enabledDeptOptions = this.filterDisabledDept(JSON.parse(JSON.stringify(response.data)))
      })

    },
    // 节点单击事件
    handleNodeClick(data) {
      this.form = data
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleResetPwd":
          this.handleResetPwd(row)
          break
        case "handleAuthRole":
          this.handleAuthRole(row)
          break
        default:
          break
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true

    },
    //提交新增表单
    submitAddForm:function() {
      //处理FID
      if(this.formAdd.fid != null &&this.formAdd.fid.length > 0){
        this.formAdd.fid = this.formAdd.fid[this.formAdd.fid.length - 1];
      }

      this.$refs["formAdd"].validate(valid => {
        if (valid) {
          addCategroy(this.formAdd).then(response => {
            this.$modal.msgSuccess("新增成功")
            this.open = false
            this.getList()
            this.formAdd = {}
          })
        }
      })

    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateCategroy(this.form).then(response => {
            this.$modal.msgSuccess("修改成功")
            this.open = false
            this.getList()
            this.form = {}
          })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete:function() {
      const name = this.form.name
      const id = this.form.id
      this.$confirm(`确定删除分类：【${name}】吗？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delCategroy(id).then(() => {
          this.$modal.msgSuccess("删除成功")
          this.open = false
          this.getList()
          this.form = {}
        })
      })
    }
  }
}
</script>
